﻿using System;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using BLL;

namespace ChamCongMain.Form
{
    public partial class frmSanPham : DevExpress.XtraEditors.XtraForm
    {
        ChucNang cn;
        public Delegates.RefeshGrid LoadData;
        long id = 0;

        public frmSanPham()
        {
            InitializeComponent();
        }
        public frmSanPham(ChucNang cn, SanPham sp)
        {
            InitializeComponent();
            LoadDataToGridLookup();
            this.cn = cn;
            if (this.cn == ChucNang.Them)
            {
                this.Text = "Thêm Sản Phẩm";
            }
            else
            {
                this.Text = "Sửa Sản Phẩm";
                SetValueForForm(sp);
            }
        }
        /// <summary>
        ///  đưa giữ liêu lên form để sửa
        /// </summary>
        /// <param name="sp"></param>
        private void SetValueForForm(SanPham sp)
        {
            txtTenSP.Text = sp.TenSanPham;
            id = sp.SanPhamId;
            glkHangSP.EditValue = sp.HangSPId;
            if (sp.SoCongDoan != null)
                nbrSoCongDoan.Value = decimal.Parse(sp.SoCongDoan.ToString());
            if (sp.GiaTien != null)
                calGiaTien.Value = decimal.Parse(sp.GiaTien.ToString());
            txtGhiChu.Text = sp.GhiChu;
        }
        /// <summary>
        /// xoas trang lai form khi them xong 
        /// </summary>
        private void ClearForm()
        {
            txtTenSP.Text = string.Empty;
            glkHangSP.EditValue = string.Empty;
            nbrSoCongDoan.Value = 0;
            calGiaTien.Value = 0;
            txtGhiChu.Text = string.Empty;
            cn = ChucNang.Them;
            this.Text = "Thêm Sản Phẩm";

        }
        /// <summary>
        /// Get all value from form
        /// </summary>
        /// <returns></returns>
        private SanPham GetValueFromForm()
        {
            SanPham sp = new SanPham();
            sp.SanPhamId = id;
            sp.TenSanPham = txtTenSP.Text;
            if (glkHangSP.EditValue != null)
                sp.HangSPId = (long)glkHangSP.EditValue;
            if (nbrSoCongDoan.Value != null)
                sp.SoCongDoan = (int)nbrSoCongDoan.Value;
            if (calGiaTien.EditValue != null)
                sp.GiaTien = double.Parse(calGiaTien.EditValue.ToString());
            sp.GhiChu = txtGhiChu.Text;
            return sp;
        }
        /// <summary>
        /// Get data to grid lookup
        /// </summary>
        private void LoadDataToGridLookup()
        {
            HangSanPhamBLL hspbll = new HangSanPhamBLL();
            glkHangSP.Properties.ValueMember = "HangSPId";
            glkHangSP.Properties.DisplayMember = "TenHang";
            glkHangSP.Properties.DataSource = hspbll.GetAllHangSanPham();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            SanPham sp = GetValueFromForm();
            SanPhamBLL spbll = new SanPhamBLL();
            if (cn == ChucNang.Them)
            {
                spbll.InsertSanPham(sp);
                LoadData(spbll.GetAllSanPham());
                XtraMessageBox.Show("Thêm thành công!","Thêm",MessageBoxButtons.OK,MessageBoxIcon.Information);
            }
            else
            {
                spbll.UpdateSanPham(sp);
                LoadData(spbll.GetAllSanPham());
                XtraMessageBox.Show("Sửa thành công!", "Thêm", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            this.Close();
        }

        private void btnLuuThem_Click(object sender, EventArgs e)
        {
            SanPham sp = GetValueFromForm();
            SanPhamBLL spbll = new SanPhamBLL();
            if (cn == ChucNang.Them)
            {
                spbll.InsertSanPham(sp);
                LoadData(spbll.GetAllSanPham());
                XtraMessageBox.Show("Thêm thành công!", "Thêm", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                spbll.UpdateSanPham(sp);
                LoadData(spbll.GetAllSanPham());
                XtraMessageBox.Show("Sửa thành công!", "Thêm", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            ClearForm();
        }

        private void btnHuyBo_Click(object sender, EventArgs e)
        {
            if(XtraMessageBox.Show("Bạn có muốn thoát không?", "Thoát", MessageBoxButtons.YesNo, MessageBoxIcon.Question)==DialogResult.Yes){
                    this.Close();
            }
        }
    }
}